package database.employee;

import com.sun.istack.internal.NotNull;
import database.CRUD;

import javax.swing.table.DefaultTableModel;
import java.sql.*;

public class tb_employee {//员工表
    @NotNull
    private String Eid;//员工编号
    @NotNull
    private String Ename;//员工姓名
    @NotNull
    private String EPas;//员工密码
    @NotNull
    private String Elevel;//员工级别
    @NotNull
    private String EtelPhone;//员工电话
    private int ESalary;//员工工资
    private String other;//备注
    public tb_employee() {}
    public tb_employee(String Eid, String Ename, String EPas, String Elevel, String EtelPhone, int ESalary, String other) {
        this.Eid = Eid;
        this.Ename = Ename;
        this.EPas = EPas;
        this.Elevel = Elevel;
        this.EtelPhone = EtelPhone;
        this.ESalary = ESalary;
        this.other = other;
    }
    public void setEid(String Eid) {
        this.Eid = Eid;
    }
    public void setEname(String Ename) {
        this.Ename = Ename;
    }
    public void setEPas(String EPas) {
        this.EPas = EPas;
    }
    public void setElevel(String Elevel) {
        this.Elevel = Elevel;
    }
    public void setEtelPhone(String EtelPhone) {
        this.EtelPhone = EtelPhone;
    }
    public void setESalary(int ESalary) {
        this.ESalary = ESalary;
    }
    public void setOther(String other) {
        this.other = other;
    }
    public String getEid() {
        return Eid;
    }
    public String getEname() {
        return Ename;
    }
    public String getEPas() {
        return EPas;
    }
    public String getElevel() {
        return Elevel;
    }
    public String getEtelPhone() {
        return EtelPhone;
    }
    public int getESalary() {
        return ESalary;
    }
    public String getOther() {
        return other;
    }


    // 插入新员工
    public boolean insertEmployee(String Eid,String Ename, String EPas, String Elevel, String EtelPhone, int ESalary, String other) {
        String sql = "INSERT INTO tb_employee (Eid, Ename, EPas, Elevel, EtelPhone, ESalary, other) "
                + "VALUES (?,?,?,?,?,?,?)";
        try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC", "root", "123456");
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            // 设置预编译语句的参数值
            pstmt.setString(1, Eid);
            pstmt.setString(2, Ename);
            pstmt.setString(3, EPas);
            pstmt.setString(4, Elevel);
            pstmt.setString(5, EtelPhone);
            pstmt.setInt(6, ESalary);
            pstmt.setString(7, other);

            // 执行更新操作
            int rowsAffected = pstmt.executeUpdate();

            if (rowsAffected > 0) {
                return true; // 插入成功
            } else {
                return false; // 插入失败
            }

        } catch (SQLException e) {
            e.printStackTrace();
            return false; // 插入失败
        }
    }

    // 更新员工信息
    public boolean updateEmployee(String Eid,String Ename, String EPas, String Elevel, String EtelPhone, int ESalary, String other) {
        String sql = "UPDATE tb_employee SET Ename = ?, EPas = ?, Elevel = ?, EtelPhone = ?, ESalary = ?, other = ? "
                + "WHERE Eid = ?";
        Object[] params = {Ename, EPas, Elevel, EtelPhone, ESalary, other, Eid}; // 参数数组
        try {
            CRUD.executeUpdate(sql);
            return true; // 更新成功
        } catch (SQLException e) {
            e.printStackTrace();
            return false; // 更新失败
        }
    }

    public void authenticate(tb_employee e1, String Eid, String EPas) {
        String sql = "SELECT * FROM tb_employee WHERE Eid = ? AND EPas = ?";

        try (Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC", "root", "123456");
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            // 设置参数
            pstmt.setString(1, Eid);
            pstmt.setString(2, EPas);

            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {
                String EName = rs.getString("EName");
                String Elevel = rs.getString("Elevel");
                String EtelPhone = rs.getString("EtelPhone");
                int ESalary = rs.getInt("ESalary");
                String other = rs.getString("other");

                // 修改传入的 tb_employee 对象的属性
                e1.setEid(Eid);
                e1.setEname(EName);
                e1.setEPas(EPas);
                e1.setElevel(Elevel);
                e1.setEtelPhone(EtelPhone);
                e1.setESalary(ESalary);
                e1.setOther(other);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    // 删除员工
    public boolean deleteEmployee(String attribute, String value) {
        String sql;
        if ("Eid".equalsIgnoreCase(attribute)) {
            //如果是Eid的的话，直接查询
            sql = "DELETE FROM tb_employee WHERE Eid = ?";
        } else {
            // 如果是其他的需要添加属性查询
            sql = "SELECT * FROM tb_employee WHERE " + attribute + " = ?";
        }

        try {
            PreparedStatement pstmt = CRUD.getConnection().prepareStatement(sql);
            pstmt.setString(1, value);

            if ("Eid".equalsIgnoreCase(attribute)) {
                int rowsAffected = pstmt.executeUpdate();
                return rowsAffected > 0;
            } else {
                //sql查询出来的行
                ResultSet rs = pstmt.executeQuery();
                int rowCount = 0;
                while (rs.next()) {
                    rowCount++;
                    String eid = rs.getString("Eid");
                    String ename = rs.getString("Ename");
                    System.out.println("找到员工：Eid=" + eid + ", Ename=" + ename);
                }

                if (rowCount == 0) {
                    System.out.println("没有找到 " + attribute + " = " + value + " 的员工");
                    return false;
                } else if (rowCount == 1) {

                    int rowsAffected = pstmt.executeUpdate();
                    return rowsAffected > 0;
                } else {
                    System.out.println("找到多个符合条件的员工 " + attribute + " = " + value);
                    return false;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false; // Delete failed
        }
    }


    // 查询员工信息
    public DefaultTableModel queryEmployees() {
        String sql = "SELECT * FROM tb_employee";
        try {
            return CRUD.executeQueryAndBuildTableModel(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            return null; // 查询失败
        }
    }
}



